home *** CD-ROM | disk | FTP | other *** search
- /* graphdmo.c - demonstration of the graphics library mgraph.c/msgraph.asm
- * This program uses functions setvid(), fcircle() in file mgraph.c,
- * pset(), line(), fline() in file msgraph.asm, and ci() in file ci.asm.
- *
- * by Paul Ketrick
- */
-
- #include <math.h> /* contains declarations for math functions */
-
- #define PI 3.14159265359
- #define sign(x) (x==0 ? 0 : (x < 0 ? -1 : 1))
-
- extern double aspect; /* video display aspect ratio - contained in
- * mgraph.c
- */
-
- main()
- {
- char c;
-
- for (;;) {
- setvid(2); /* initialize 80x25 text mode */
- printf("Graphics demonstration program.\n\n");
- printf("A - Polygon drawing (640x200 mode)\n");
- printf("B - Polygon drawing (320x200 mode)\n");
- printf("C - Line-drawing\n");
- printf("D - 160x100 Rainbow\n");
- printf("E - 160x100 Color spectrum\n");
- printf("F - Circle-drawing\n");
- printf("X - Exit\n");
- printf("Please enter your selection:");
-
- for (c=0; c== 0;) {
- while ((c=ci()) == -1) /* wait for a key to be pressed */
- ;
- switch(tolower(c)) { /* call the selected routine */
- case 'a':
- polygon6();
- break;
-
- case 'b':
- polygon3();
- break;
-
- case 'c':
- lines();
- break;
-
- case 'd':
- rainbow();
- break;
-
- case 'e':
- spectrum();
- break;
-
- case 'f':
- circles();
- break;
-
- case 'x':
- exit(0);
-
- default:
- c=0;
- }
- }
- }
- }
-
- polygon6()
- {
- int sides, i, j;
- int x[50], y[50];
- double deg;
-
- printf("\nEnter the number of sides to use:");
- scanf("%d", &sides);
-
- setvid(6); /* initialize 640x200 mode */
-
- i=0;
- /* build array containing coordinates for each point on the polygon's
- * perimeter:
- */
- for (deg=0; deg < 2*PI; deg+=2*PI / sides) {
- x[i]=320 + aspect * 99 * cos(deg);
- y[i]=100 + 99 * sin(deg);
- ++i;
- }
-
- /* draw a line from every point to every other point: */
- for (i=0; i < sides-1; i++)
- for (j=i+1; j < sides; j++)
- fline(x[i], y[i], x[j], y[j], 1);
-
- while (ci() == -1)
- ;
- }
-
- polygon3()
- {
- int sides, i, j;
- double deg, aspect;
- int x[50], y[50];
-
- printf("\nEnter the number of sides to use:");
- scanf("%d", &sides);
-
- aspect=1.0; /* aspect ratio for my monitor */
- i=0;
- for (deg=0; deg < 2*PI; deg+=2*PI / sides) {
- x[i]=160 + aspect * 99 * cos(deg);
- y[i]=100 + 99 * sin(deg);
- ++i;
- }
-
- setvid(4); /* initialize 320x200 graphics mode */
-
- for (i=0; i < sides-1; i++)
- for (j=i+1; j < sides; j++)
- fline(x[i], y[i], x[j], y[j], 1);
-
- while (ci() == -1)
- ;
- }
-
- double rnd(); /* source code for this function is below */
-
- #define NUMLINES 150
-
- lines() /* this one works really well only on a system equipped
- * with a math coprocessor - this is due to the slow
- * random number generator
- */
- {
- static int x1[NUMLINES], y1[NUMLINES], x2[NUMLINES], y2[NUMLINES];
- int in=1, out=2, i, j, x=0, y=0, x0=0, y0=0, dx=2, dy=3, dx0=1, dy0=-1;
-
- while (x < 2 || x > 637 || y < 2 || y > 197) {
- printf("\nEnter the beginning coordinates for x1 and y1:");
- scanf("%d,%d", &x, &y);
- }
-
- while (x0 < 2 || x0 > 637 || y0 < 2 || y0 > 197) {
- printf("Enter the beginning coordinates for x2 and y2:");
- scanf("%d,%d", &x0, &y0);
- }
-
- setvid(6); /* initialize 640x200 graphics mode */
-
- for (i=0; i<NUMLINES; ++i)
- x1[i]=y1[i]=x2[i]=y2[i]=0;
-
- while (ci() == -1) {
- while ((x+=dx) < 0 || x > 639) {
- dx=(x < 320 ? 1 : -1) * rnd() * 5;
- }
-
- while ((y+=dy) < 0 || y > 199) {
- dy=(y < 100 ? 1 : -1) * rnd() * 3;
- }
-
- while ((x0+=dx0) < 0 || x0 > 639) {
- dx0=(x0 < 320 ? 1 : -1) * rnd() * 5;
- }
-
- while ((y0+=dy0) < 0 || y0 > 199) {
- dy0=(y0 < 100 ? 1 : -1) * rnd() * 3;
- }
-
- x1[in]=x;
- y1[in]=y;
- x2[in]=x0;
- y2[in]=y0;
- fline(x, y, x0, y0, 1);
- fline(x1[out], y1[out], x2[out], y2[out], 0);
-
- ++out;
- if (out > NUMLINES)
- out=0;
-
- ++in;
- if (in > NUMLINES)
- in=0;
- }
- }
-
- rainbow() /* also only really good on 8087 systems */
- {
- int i, color;
- float deg, inc;
-
- setvid(8);
-
- for (i=25; i < 79; ++i) {
- inc=PI/4 / i;
- for (deg=0; deg < PI; deg+=inc)
- pset(80 + (int)(i * cos(deg)), 82 - (int)(i * sin(deg)), color);
- color = (color==15) ? 1 : ((color==7) ? 9 : color+1);
- }
-
- while (ci() == -1)
- ;
- }
-
- double rnd() /* fairly good (?) pseudo-random number generator
- * returns a double-precision number between 0 and 1
- */
- {
- static double seed;
- double num;
-
- seed=cos(seed * seed) / log(seed + 2);
- num=seed * 1000;
- return(num - floor(num));
- }
-
- spectrum() /* display the 160x100 graphics mode's color spectrum */
- {
- int i, j;
-
- setvid(8); /* initialize 160x100 b/w mode */
-
- for (j=0; j < 40; ++j)
- for (i=0; i < 160; ++i)
- pset(i, j, i+j);
-
- for (i=0; i < 16; ++i)
- fillbox(i*10, 50, i*10+8, 99, i);
-
- while (ci() == -1)
- ;
- }
-
- circles() /* draws neat circle patterns */
- {
- double angle;
- int c;
-
- setvid(6); /* initialize 640x200 mode */
- for (c=0; c<10; ++c) {
- for (angle=0.0; angle<6.0*PI; angle+=PI/20.0)
- fcircle((int)(148.0*cos(angle)+320.0), (int)(74.0*sin(angle)+100.0),
- (int)(angle*4.0/PI), 1);
-
- for (angle=0.0; angle<6.0*PI; angle+=PI/20.0)
- fcircle((int)(148.0*cos(angle)+320.0), (int)(74.0*sin(angle)+100.0),
- (int)(angle*4.0/PI), 0);
- }
- setvid(2);
- }
-